package com.example.videopublish.activity

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.ImageView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.example.videopublish.R
import com.example.videopublish.model.VideoModel
import com.example.videopublish.service.DouyinRpaService

/**
 * 视频编辑活动
 * 允许用户编辑视频元数据并发布到抖音
 */
class VideoEditActivity : AppCompatActivity() {

    companion object {
        const val EXTRA_VIDEO_URI = "extra_video_uri"
        const val EXTRA_VIDEO_DURATION = "extra_video_duration"
        const val EXTRA_VIDEO_SIZE = "extra_video_size"
        const val EXTRA_THUMBNAIL_PATH = "extra_thumbnail_path"
    }

    private lateinit var videoThumbnailImageView: ImageView
    private lateinit var titleEditText: EditText
    private lateinit var descriptionEditText: EditText
    private lateinit var tagsEditText: EditText
    private lateinit var publishButton: Button

    private lateinit var videoModel: VideoModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_video_edit)

        // 从Intent获取视频数据
        getVideoDataFromIntent()
        
        // 初始化视图
        initViews()
        
        // 设置发布按钮点击监听
        setupPublishButton()
    }

    /**
     * 从Intent获取视频数据
     */
    private fun getVideoDataFromIntent() {
        // 获取URI字符串
        val uriString = intent.getStringExtra(EXTRA_VIDEO_URI)
        if (uriString == null) {
            Toast.makeText(this, "无效的视频数据", Toast.LENGTH_SHORT).show()
            finish()
            return
        }
        
        // 转换为URI对象
        val uri = Uri.parse(uriString)
        
        // 获取其他视频数据
        val duration = intent.getLongExtra(EXTRA_VIDEO_DURATION, 0)
        val size = intent.getLongExtra(EXTRA_VIDEO_SIZE, 0)
        val thumbnailPath = intent.getStringExtra(EXTRA_THUMBNAIL_PATH)

        // 创建视频模型
        videoModel = VideoModel(
            uri = uri,
            durationMs = duration,
            sizeBytes = size,
            thumbnailPath = thumbnailPath
        )
    }

    /**
     * 初始化视图
     */
    private fun initViews() {
        videoThumbnailImageView = findViewById(R.id.videoThumbnailImageView)
        titleEditText = findViewById(R.id.titleEditText)
        descriptionEditText = findViewById(R.id.descriptionEditText)
        tagsEditText = findViewById(R.id.tagsEditText)
        publishButton = findViewById(R.id.publishButton)

        // 加载视频缩略图
        videoModel.thumbnailPath?.let {
            videoThumbnailImageView.setImageURI(Uri.parse(it))
        }
    }

    /**
     * 设置发布按钮点击监听
     */
    private fun setupPublishButton() {
        publishButton.setOnClickListener {
            // 收集用户输入的数据
            collectUserInput()
            
            // 启动抖音发布流程
            startDouyinPublishProcess()
        }
    }

    /**
     * 收集用户输入的数据
     */
    private fun collectUserInput() {
        videoModel.title = titleEditText.text.toString().trim()
        videoModel.description = descriptionEditText.text.toString().trim()
        videoModel.setTagsFromString(tagsEditText.text.toString().trim())
        
        // 使用默认值，如果用户未输入
        if (videoModel.title.isEmpty()) {
            videoModel.title = "我的视频 - ${System.currentTimeMillis()}"
        }
    }

    /**
     * 启动抖音发布流程
     */
    private fun startDouyinPublishProcess() {
        // 检查输入是否有效
        if (!validateInput()) {
            return
        }
        
        // 提示用户
        Toast.makeText(this, "准备发布到抖音，请稍后...", Toast.LENGTH_SHORT).show()
        
        // 将发布数据传递给RPA服务
        val intent = Intent(this, DouyinRpaService::class.java).apply {
            action = DouyinRpaService.ACTION_PUBLISH
            putExtra(DouyinRpaService.EXTRA_VIDEO_URI, videoModel.uri.toString())
            putExtra(DouyinRpaService.EXTRA_VIDEO_TITLE, videoModel.title)
            putExtra(DouyinRpaService.EXTRA_VIDEO_DESCRIPTION, videoModel.description)
            putExtra(DouyinRpaService.EXTRA_VIDEO_TAGS, videoModel.getTagsAsString())
        }
        startService(intent)
        
        // 禁用发布按钮，防止重复点击
        publishButton.isEnabled = false
        publishButton.text = getString(R.string.publishing)
    }

    /**
     * 验证用户输入
     */
    private fun validateInput(): Boolean {
        // 标题不能为空
        if (titleEditText.text.toString().trim().isEmpty()) {
            titleEditText.error = "请输入视频标题"
            titleEditText.requestFocus()
            return false
        }
        
        return true
    }
} 